package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.domain.FlyToLine;
import com.landinfo.service.FlyToLineService;
import com.landinfo.web.rest.errors.BadRequestAlertException;
import com.landinfo.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing FlyToLine.
 */
@RestController
@RequestMapping("/api")
public class FlyToLineResource {

    private final Logger log = LoggerFactory.getLogger(FlyToLineResource.class);

    private static final String ENTITY_NAME = "flyToLine";

    private final FlyToLineService flyToLineService;

    public FlyToLineResource(FlyToLineService flyToLineService) {
        this.flyToLineService = flyToLineService;
    }

    /**
     * POST  /fly-to-lines : Create a new flyToLine.
     *
     * @param flyToLine the flyToLine to create
     * @return the ResponseEntity with status 201 (Created) and with body the new flyToLine, or with status 400 (Bad Request) if the flyToLine has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/fly-to-lines")
    @Timed
    public ResponseEntity<FlyToLine> createFlyToLine(@Valid @RequestBody FlyToLine flyToLine) throws URISyntaxException {
        log.debug("REST request to save FlyToLine : {}", flyToLine);
        if (flyToLine.getId() != null) {
            throw new BadRequestAlertException("A new flyToLine cannot already have an ID", ENTITY_NAME, "idexists");
        }
        FlyToLine result = flyToLineService.save(flyToLine);
        return ResponseEntity.created(new URI("/api/fly-to-lines/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /fly-to-lines : Updates an existing flyToLine.
     *
     * @param flyToLine the flyToLine to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated flyToLine,
     * or with status 400 (Bad Request) if the flyToLine is not valid,
     * or with status 500 (Internal Server Error) if the flyToLine couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/fly-to-lines")
    @Timed
    public ResponseEntity<FlyToLine> updateFlyToLine(@Valid @RequestBody FlyToLine flyToLine) throws URISyntaxException {
        log.debug("REST request to update FlyToLine : {}", flyToLine);
        if (flyToLine.getId() == null) {
            return createFlyToLine(flyToLine);
        }
        FlyToLine result = flyToLineService.save(flyToLine);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, flyToLine.getId().toString()))
            .body(result);
    }

    /**
     * GET  /fly-to-lines : get all the flyToLines.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of flyToLines in body
     */
    @GetMapping("/fly-to-lines")
    @Timed
    public List<FlyToLine> getAllFlyToLines() {
        log.debug("REST request to get all FlyToLines");
        return flyToLineService.findAll();
        }

    /**
     * GET  /fly-to-lines/:id : get the "id" flyToLine.
     *
     * @param id the id of the flyToLine to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the flyToLine, or with status 404 (Not Found)
     */
    @GetMapping("/fly-to-lines/{id}")
    @Timed
    public ResponseEntity<FlyToLine> getFlyToLine(@PathVariable Long id) {
        log.debug("REST request to get FlyToLine : {}", id);
        FlyToLine flyToLine = flyToLineService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(flyToLine));
    }

    /**
     * DELETE  /fly-to-lines/:id : delete the "id" flyToLine.
     *
     * @param id the id of the flyToLine to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/fly-to-lines/{id}")
    @Timed
    public ResponseEntity<Void> deleteFlyToLine(@PathVariable Long id) {
        log.debug("REST request to delete FlyToLine : {}", id);
        flyToLineService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
